<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>首页</title>
    <script src="js/jquery-3.5.1.js"></script>
    <script type="text/javascript" src="js/base.js"></script>
</head>
<body>
<table align="center" border="2">
    <caption>用户列表</caption>
    <tr>
        <td>序号</td>
        <td>用户名</td>
        <td>密码</td>
        <td>昵称</td>
        <td>年龄段</td>
        <td>生日</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${userMap}" var="entry" varStatus="status">
        <tr>
            <td>${status.index+1}</td>
            <td>${entry.key}</td>
            <td>${entry.value.password}</td>
            <td>${entry.value.nickname}</td>
            <td>
                <c:choose>
                    <c:when test="${entry.value.age<18}">未成年</c:when>
                    <c:when test="${entry.value.age<30}">青年</c:when>
                    <c:when test="${entry.value.age<60}">中年</c:when>
                    <c:otherwise>老年</c:otherwise>
                </c:choose>
            </td>
            <td><fmt:formatDate value="${entry.value.birthday}" pattern="yyyy年MM月dd日"/></td>
            <td><span style="cursor: pointer;padding: 0 5px" id="del" onclick="del('${entry.key}')"
                      data-username="${entry.key}">删除</span><span style="cursor: pointer;padding: 0 5px" id="modify"
                                                                  onclick="modify('${entry.key}')"
                                                                  data-username="${entry.key}">编辑</span></td>
        </tr>
    </c:forEach>
</table>
<script>
    function del(username) {
        $.ajax({
            url: baseUrl + "/ajax_del?username=" + username,
            type: "delete",
            success: function (res) {
                console.log(res)
                if (res.data == true) {
                    alert("删除成功")
                    location.reload()
                } else {
                    alert(res.msg)
                }
            },
            error: function (e) {
                console.log(e)
            }
        })
    }

    function modify(username) {
        window.location.href = "register.jsp?username=" + username
    }
</script>
</body>
</html>
